library(plotly)
## Warning: package 'plotly' was built under R version 3.6.2
## Loading required package: ggplot2
##
## Attaching package: 'plotly'
## The following object is masked from 'package:ggplot2':
##
## last_plot
## The following object is masked from 'package:stats':
##
## filter
## The following object is masked from 'package:graphics':
##
## layout
library(tidyverse)
## -- Attaching packages -------------------------------------------------------------------------- tidyverse 1.2.1 --
## v tibble 2.1.3 v purrr 0.3.2
## v tidyr 0.8.3 v dplyr 0.8.3
## v readr 1.3.1 v stringr 1.4.0
## v tibble 2.1.3 v forcats 0.4.0
## -- Conflicts ----------------------------------------------------------------------------- tidyverse_conflicts() --
## x dplyr::filter() masks plotly::filter(), stats::filter()
## x dplyr::lag() masks stats::lag()
head(diamonds)
diamonds.sample <- diamonds %>%
sample_n(10000)
plot_ly(data = diamonds, x= ~carat, y = ~price)
## No trace type specified:
## Based on info supplied, a 'scatter' trace seems appropriate.
## Read more about this trace type -> https://plot.ly/r/reference/#scatter
## No scatter mode specifed:
## Setting the mode to markers
## Read more about this attribute -> https://plot.ly/r/reference/#scatter-mode
plot_ly(data = diamonds, x= ~cut, y = ~price)
## No trace type specified:
## Based on info supplied, a 'bar' trace seems appropriate.
## Read more about this trace type -> https://plot.ly/r/reference/#bar
diamonds %>%
sample_n(100) %>%
plot_ly(data = ., x= ~carat, y = ~price, type = "scatter")
## No scatter mode specifed:
## Setting the mode to markers
## Read more about this attribute -> https://plot.ly/r/reference/#scatter-mode
plot_ly(data = diamonds, x= ~carat, y = ~price) %>%
add_trace(p = ., type = "scatter")
## No scatter mode specifed:
## Setting the mode to markers
## Read more about this attribute -> https://plot.ly/r/reference/#scatter-mode
Styling
plot_ly(data = diamonds.sample, x= ~carat, y = ~price, text = ~paste0("Price: $", price, "<br>Carat:", carat)) %>%
add_trace(p = ., type = "scatter") %>%
layout(xaxis = list(range = c(2.5, 5), title = "Carat"),
yaxis = list(title = "price"))
## No scatter mode specifed:
## Setting the mode to markers
## Read more about this attribute -> https://plot.ly/r/reference/#scatter-mode
lines.data <- data.frame(x = c(1:100),
trace0 = rnorm(100, mean = 5)
) %>%
mutate(trace1 = log(trace0),
trace2 = trace0^2)
gather(data = lines.data, key = "trace", value = "val", -x)
lines.data.wide <- gather(data = lines.data, key = "trace", value = "val", -x)
plot_ly(data = lines.data, x = ~x) %>%
add_trace(y = ~trace0, name = "Trace Zero", mode = 'lines') %>%
add_trace(y = ~trace1, name = "Trace One", mode = 'lines+marker') %>%
add_trace(y = ~trace2, name = "Trace Two", mode = 'marker')
## No trace type specified:
## Based on info supplied, a 'scatter' trace seems appropriate.
## Read more about this trace type -> https://plot.ly/r/reference/#scatter
## No trace type specified:
## Based on info supplied, a 'scatter' trace seems appropriate.
## Read more about this trace type -> https://plot.ly/r/reference/#scatter
## No trace type specified:
## Based on info supplied, a 'scatter' trace seems appropriate.
## Read more about this trace type -> https://plot.ly/r/reference/#scatter
plot_ly(data = lines.data.wide, x = ~x, y = ~val, color = ~trace, mode = "lines+markers") %>%
layout(xaxis = list(ticks = "Outside",
dtick = 10,
ticklen = 5,
tickwidth = 2,
tickcolor = toRGB("pink"),
title = "X",
showgrid = FALSE,
zeroline = FALSE),
yaxis = list(ticks = "Outside",
dtick = 5,
ticklen = 10,
tickwidth = 1,
tickcolor = toRGB("pink"),
title = "Val",
showgrid = FALSE,
zeroline = FALSE),
showlegend = TRUE,
legend = list(x = .15, y = .95),
title = "Fake Data")
## No trace type specified:
## Based on info supplied, a 'scatter' trace seems appropriate.
## Read more about this trace type -> https://plot.ly/r/reference/#scatter
cnt <- with(diamonds.sample, table(cut, clarity))
plot_ly(data = diamonds.sample, x= ~cut, y = ~clarity, z = ~cnt) %>%
add_trace(type = "histogram2dcontour",
contours = list(showlabels = T, labelfont = list(color = 'white')))
plot_ly(data = diamonds.sample, y = ~price, color = ~cut, type = "box")
plot_ly(data = diamonds.sample, y = ~price, color = ~cut, type = "box")
plot_ly(data = diamonds.sample, x = ~carat, y = ~price, z = ~depth)
## No trace type specified:
## Based on info supplied, a 'scatter3d' trace seems appropriate.
## Read more about this trace type -> https://plot.ly/r/reference/#scatter3d
## No scatter3d mode specifed:
## Setting the mode to markers
## Read more about this attribute -> https://plot.ly/r/reference/#scatter-mode
d.fig.1 <- diamonds.sample %>%
ggplot(data = ., aes(x = price, y = depth, color = cut)) +
geom_point() +
stat_smooth()
ggplotly(d.fig.1)
## `geom_smooth()` using method = 'gam' and formula 'y ~ s(x, bs = "cs")'